package com.jiangman.weather.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jiangman.weather.R;
import com.jiangman.weather.bean.CommonResponse;
import com.jiangman.weather.callback.CustomCallback;
import com.jiangman.weather.callback.RsponseStateListenerImp;
import com.jiangman.weather.constant.ConstantSp;
import com.jiangman.weather.constant.ConstantUrl;
import com.jiangman.weather.view.EditTextWithDel;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import util.FastBlurUtil;
import util.ToastUitl;

public class ChangPwdActivity extends AppCompatActivity {


    @BindView(R.id.old_pwd)
    EditTextWithDel oldPwd;
    @BindView(R.id.new_first_pwd)
    EditTextWithDel newFirstPwd;
    @BindView(R.id.new_second_pwd)
    EditTextWithDel newSecondPwd;
    @BindView(R.id.bt_commit)
    Button btCommit;
    @BindView(R.id.ll_change_pwd)
    LinearLayout llChangePwd;
    @BindView(R.id.iv_back)
    ImageView ivBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chang_pwd);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        int scaleRatio = 50;//可以设置模糊度哦
        int blurRadius = 8;
        Bitmap originBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.login_bg);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(originBitmap,
                originBitmap.getWidth() / scaleRatio,
                originBitmap.getHeight() / scaleRatio,
                false);
        Bitmap blurBitmap = FastBlurUtil.doBlur(scaledBitmap, blurRadius, true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            llChangePwd.setBackground(new BitmapDrawable(blurBitmap));
        }
    }


    @OnClick(R.id.bt_commit)
    public void onClick2() {
        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl + "Weather/regist")
                .addParams("user_name",ConstantSp.USER_NAME)
                .addParams("user_old_pwd", oldPwd.getText().toString())
                .addParams("user_new_pwd", newSecondPwd.getText().toString())
                .addParams("id",ConstantSp.USER_ID)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<CommonResponse>() {

                    @Override
                    public void fail(CommonResponse response) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void success(CommonResponse response, boolean isRefresh) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void error(Exception e) {
                        System.out.println(e.toString());
                    }
                }, CommonResponse.class, false));
    }



    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }
}
